using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Acme.BookStore.Domain.Entities;
using Acme.BookStore.Shops.Dtos;
using Acme.BookStore.Shops;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Microsoft.Extensions.Logging;

namespace Acme.BookStore.Shops
{
    //[Authorize(BookStorePermissions.Shops.Default)]
    public class ShopAppService : 
        ApplicationService, // 不再继承 CrudAppService
        IShopAppService
    {
        private readonly IShopRepository _shopRepository;
        private readonly ILogger<ShopAppService> _logger;

        public ShopAppService(IShopRepository repository, ILogger<ShopAppService> logger)
        {
            _shopRepository = repository;
            _logger = logger;
        }

        public async Task<PagedResultDto<ShopDto>> GetListAsync([FromQuery] PagedAndSortedResultRequestDto input)
        {
            var totalCount = await _shopRepository.GetCountAsync(null);

            var shops = await _shopRepository.GetListAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "Id",
                null
            );

            return new PagedResultDto<ShopDto>(
                totalCount,
                ObjectMapper.Map<List<Shop>, List<ShopDto>>(shops)
            );
        }

        public async Task<ShopDto> GetAsync(string id)
        {
            var shop = await _shopRepository.GetAsync(id);
            return ObjectMapper.Map<Shop, ShopDto>(shop);
        }

        public async Task<ShopDto> CreateAsync([FromBody] CreateShopDto input)
        {
            var id = Guid.NewGuid().ToString("N");
            var shop = new Shop(id)
            {
                ProductName = input.ProductName,
                EnglishName = input.EnglishName,
                JapaneseName = input.JapaneseName,
                Specification = input.Specification,
                OwnerCode = input.OwnerCode,
                CustomerProductCode = input.CustomerProductCode,
                ProductAttribute = input.ProductAttribute,
                Category = input.Category,
                ProductType = input.ProductType,
                SplitControl = input.SplitControl,
                TrayLayerCount = input.TrayLayerCount,
                TrayHeight = input.TrayHeight,
                BillingType = input.BillingType,
                Barcode = input.Barcode,
                ShelfLifeDaysUnit = input.ShelfLifeDaysUnit,
                SplitUnit = input.SplitUnit,
                Volume = input.Volume,
                SplitQuantity = input.SplitQuantity,
                Price = input.Price,
                Length = input.Length,
                Width = input.Width,
                Height = input.Height,
                Weight = input.Weight,
                Tingyong = input.Tingyong,
                Sku = input.Sku
            };
            var createdShop = await _shopRepository.InsertAsync(shop, autoSave: true);
            return ObjectMapper.Map<Shop, ShopDto>(createdShop);
        }

        public async Task<ShopDto> UpdateAsync([FromRoute] string id, [FromBody] CreateShopDto input)
        {
            var shop = await _shopRepository.GetAsync(id);
            
            shop.ProductName = input.ProductName;
            shop.EnglishName = input.EnglishName;
            shop.JapaneseName = input.JapaneseName;
            shop.Specification = input.Specification;
            shop.OwnerCode = input.OwnerCode;
            shop.CustomerProductCode = input.CustomerProductCode;
            shop.ProductAttribute = input.ProductAttribute;
            shop.Category = input.Category;
            shop.ProductType = input.ProductType;
            shop.SplitControl = input.SplitControl;
            shop.TrayLayerCount = input.TrayLayerCount;
            shop.TrayHeight = input.TrayHeight;
            shop.BillingType = input.BillingType;
            shop.Barcode = input.Barcode;
            shop.ShelfLifeDaysUnit = input.ShelfLifeDaysUnit;
            shop.SplitUnit = input.SplitUnit;
            shop.Volume = input.Volume;
            shop.SplitQuantity = input.SplitQuantity;
            shop.Price = input.Price;
            shop.Length = input.Length;
            shop.Width = input.Width;
            shop.Height = input.Height;
            shop.Weight = input.Weight;
            shop.Tingyong = input.Tingyong;
            shop.Sku = input.Sku;

            var updatedShop = await _shopRepository.UpdateAsync(shop, autoSave: true);
            return ObjectMapper.Map<Shop, ShopDto>(updatedShop);
        }

        public async Task DeleteAsync([FromRoute] string id)
        {
            await _shopRepository.DeleteAsync(id);
        }

        [HttpGet("api/app/shop/search")]
        public async Task<PagedResultDto<ShopDto>> SearchAsync(ShopSearchDto input)
        {
            _logger.LogInformation($"正在搜索商品，关键词: {input.Filter}, 商品名称: {input.ProductName}");
            
            var totalCount = await _shopRepository.GetCountAsync(
                input.Filter,
                input.ProductName,
                input.ProductCode,
                input.Category,
                input.ProductType,
                input.OwnerCode
            );

            var shops = await _shopRepository.GetListAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "Id",
                input.Filter,
                input.ProductName,
                input.ProductCode,
                input.Category,
                input.ProductType,
                input.OwnerCode
            );

            _logger.LogInformation($"搜索商品成功，总数: {totalCount}, 返回: {shops.Count}");
            return new PagedResultDto<ShopDto>(
                totalCount,
                ObjectMapper.Map<List<Shop>, List<ShopDto>>(shops)
            );
        }
        
        [HttpGet("api/app/shop/search-by-name")]
        public async Task<PagedResultDto<ShopDto>> SearchByNameAsync(ShopNameSearchDto input)
        {
            _logger.LogInformation($"正在按名称搜索商品，关键词: {input.NameKeyword}");
            
            var totalCount = await _shopRepository.GetCountByNameAsync(input.NameKeyword);

            var shops = await _shopRepository.SearchByNameAsync(
                input.SkipCount,
                input.MaxResultCount,
                input.Sorting ?? "Id",
                input.NameKeyword
            );

            _logger.LogInformation($"按名称搜索商品成功，总数: {totalCount}, 返回: {shops.Count}");
            return new PagedResultDto<ShopDto>(
                totalCount,
                ObjectMapper.Map<List<Shop>, List<ShopDto>>(shops)
            );
        }
    }
} 